<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    .sel {
        background: hotpink;
    }

    li {
        margin: 10px;
    }
</style>

<body>
    <div id="app">
        <button @click="up">上移</button>
        <button @click="down">下移</button>
        <ul>
            <li :class="{sel:select.includes(index)}" v-for="(item,index) in arr" @click="sel(index)">{{item.hero}}
            </li>
        </ul>
    </div>
</body>

</html>
<script src='https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js'></script>
<script>
    let vm = new Vue({
        el: "#app",
        data: {
            select: [],
            selectIndex: [],
            arr: [
                {
                    id: 1,
                    hero: "猴子"
                },
                {
                    id: 2,
                    hero: "秃子"
                },
                {
                    id: 3,
                    hero: "菜菜"
                },
                {
                    id: 4,
                    hero: "本命"
                },
                {
                    id: 5,
                    hero: "叠毒的"
                }
            ]
        },
        methods: {
            //选中的数据
            sel(index) {
                if (this.select.includes(index)) {
                    // console.log(index)
                    this.select.splice(this.select.indexOf(index), 1)
                } else {
                    this.select.push(index)
                }
                // console.log(this.select)
            },
            //上移
            up() {
                //被选中的数组 先进行从小到大排序以免出错
                let select = this.select.sort();
                //整个列表数据对象
                let arr = this.arr;
                // 到顶部
                if (select[0] === 0) {
                    alert('已经到顶部了')
                    return
                }
                select.forEach((i, index) => {
                    arr.splice(i, 1, ...arr.splice(i - 1, 1, arr[i]))
                    select[index] = i - 1
                })
            },
            //下移
            down() {
                //下移 按照从大到小的顺序进行排列
                let select = this.select.sort((a, b) => {
                    return b - a
                });
                console.log(select)
                //整个列表数据对象
                let arr = this.arr;
                //到底部
                if (select[0] === this.arr.length - 1) {
                    alert('到底部了')
                    return
                }

                select.forEach((i, index) => {
                    arr.splice(i, 1, ...arr.splice(i + 1, 1, arr[i]))
                    select[index] = i + 1
                })
            }
        }
    })
</script>